

<?php 
 $id_lich=$_POST["id_lich"];
 $id_may=$_POST["id_may"];
 $id_user=$_POST["id_user"];
 $title=$_POST["title"];
 $content=$_POST["content"];
 $date_start=$_POST["date_start"];
 $date_end=$_POST["date_end"];
$time_s=$_POST["time_s"];
$time_e=$_POST["time_e"];

//echo $time_s."/".$time_e;
//return;

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );

global $wpdb;

/*
echo $id_lich."/";
echo $title."/";
echo $content."/";
echo $date_start."/";
echo $date_end."/";
*/
date_default_timezone_set('Asia/Ho_Chi_Minh');

//thiết lập định dạng ngày giờ 
$datetime = new DateTime();
// echo $datetime->format('Y-m-d H:i:s');

// echo "////////////////";
$time_end=strtotime($date_end);
$time_start= strtotime($date_start);

 //echo "////////////////";
 $getdate= strtotime($datetime->format('Y-m-d H:i:s'));

// echo "////////////////";

// if( $getdate > $time_start){
//  	echo "1";
//  	return;
//  }

 $query="SELECT * FROM wp_lich WHERE `ngay_batdau` = "."'".$time_s."'"." and `ngay_ket_thuc` = "."'".$time_e."'"." and id_lich =".$id_lich ." and id_may=".$id_may;
//echo $query;
//return;

$wpdb->get_results($query);
if($wpdb->num_rows!=0){
$time_batdau_cu=strtotime($time_s);
$time_kethuc_cu=strtotime($time_e);
$time_end=strtotime($date_end);
$time_start= strtotime($date_start);
			
			// Thời gian kết thúc và thoi gian bat dau thay doi . Trong khoang bat dau va ket thuc
			if($time_batdau_cu <= $time_start && $time_kethuc_cu >= $time_end){
			$query="
			UPDATE `wp_lich` SET `ngay_batdau` = "."'".$date_start."'".", `ngay_ket_thuc` = "."'".$date_end."'".", `tieu_de_lich` = "."'".$title."'".", `noi_dung_lich` = "."'".$content."'".", `ngay_sua` = "."'".$datetime->format('Y-m-d H:i:s')."'"." WHERE id_lich =".$id_lich ." and id_may=".$id_may ." and ngay_batdau ="."'".$time_s."'"." and ngay_ket_thuc ="."'".$time_e."'".""	;		
			$wpdb->query($query)or die(mysql_error());
			echo "3";
			return;
			}
			// Thời gian kết thúc thay đổi , thời gian bắt đầu giữ nguyên
			if($time_batdau_cu == $time_start && $time_kethuc_cu < $time_end){

				$date = new DateTime($time_e);
				$time_end_update= $date->format('Y')."-".$date->format('m')."-".$date->format('d')."-".$date->format('H').":".$date->format('i').":".($date->format('s')+1);
					//echo $time_end_update;

				$query="select * from wp_lich where
					 `ngay_batdau` between  "."'".$time_end_update."'"." and "."'".$date_end."'"." and id_may=".$id_may." or `ngay_ket_thuc` between  "."'".$time_end_update."'"." and "."'".$date_end."'"." and id_may=".$id_may;					 
					 //echo $query;
					 //return;
					$wpdb->get_results($query);
					if($wpdb->num_rows!=0){

						echo "2";
						return;
					}else{
						$query="
						UPDATE `wp_lich` SET `ngay_batdau` = "."'".$date_start."'".", `ngay_ket_thuc` = "."'".$date_end."'".", `tieu_de_lich` = "."'".$title."'".", `noi_dung_lich` = "."'".$content."'".", `ngay_sua` = "."'".$datetime->format('Y-m-d H:i:s')."'"." WHERE id_lich =".$id_lich ." and id_may=".$id_may ." and ngay_batdau ="."'".$time_s."'"." and ngay_ket_thuc ="."'".$time_e."'".""	;
					//echo $query;
					//return;
						$wpdb->query($query)or die(mysql_error());
						echo "3";
						return;
					}


			}


			$date = new DateTime($time_s);

				$time_start_update= $date->format('Y')."-".$date->format('m')."-".$date->format('d')."-".$date->format('H').":".$date->format('i').":".($date->format('s')-1);
			// Thời gian bắt đầu thay đổi , thời gian kết thúc giữ nguyên
			if($time_batdau_cu > $time_start && $time_kethuc_cu==$time_end){

				$query="select * from wp_lich where
					 `ngay_batdau` between  "."'".$date_start."'"." and "."'".$time_start_update."'"." and id_may=".$id_may." and id_may=".$id_may. " or `ngay_ket_thuc` between  "."'".$date_start."'"." and "."'".$time_start_update."'"." and id_may=".$id_may." and id_may=".$id_may;		;		

					 //echo $query;
					 //return;
					 //
					$wpdb->get_results($query);
					if($wpdb->num_rows!=0){
						echo "2";
						return;
					}else{
						$query="
						UPDATE `wp_lich` SET `ngay_batdau` = "."'".$date_start."'".", `tieu_de_lich` = "."'".$title."'".", `noi_dung_lich` = "."'".$content."'".", `ngay_sua` = "."'".$datetime->format('Y-m-d H:i:s')."'"." WHERE id_lich =".$id_lich ." and id_may=".$id_may ." and ngay_batdau ="."'".$time_s."'"." and ngay_ket_thuc ="."'".$time_e."'".""	;
					//echo $query;
					//return;
						$wpdb->query($query)or die(mysql_error());
						echo "3";
						return;
					}

			}
}



$query="select * from wp_lich where
 `ngay_batdau` between "."'".$date_start."'"." and "."'".$date_end."'"." and id_may=".$id_may."
or `ngay_ket_thuc` between  "."'".$date_start."'"." and "."'".$date_end."'"." and id_may=".$id_may	;
$wpdb->get_results($query);
if($wpdb->num_rows!=0){

	echo "2";
	return;
}

$query="
UPDATE `wp_lich` SET `ngay_batdau` = "."'".$date_start."'".", `ngay_ket_thuc` = "."'".$date_end."'".", `tieu_de_lich` = "."'".$title."'".", `noi_dung_lich` = "."'".$content."'".", `ngay_sua` = "."'".$datetime->format('Y-m-d H:i:s')."'"." WHERE id_lich =".$id_lich ." and id_may=".$id_may	;

$wpdb->query($query)or die(mysql_error());
//echo $query;

echo "3";
return;

